﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.DBUtility;
using System.Data;
using StoragePeriod.IDAL.StorageForecast;
using System.Data.SqlClient;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.SqlServerDAL.StorageForecast
{
    /// <summary>
    /// 数据访问类:RawNorm
    /// </summary>
    public partial class D_RawNorm : I_RawNorm
    {
        public D_RawNorm()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RawNorm");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_RawNorm model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RawNorm(");
            strSql.Append("RawId,OneNormName,NormId,TwoNormName,ElectValue,Temp1,Temp2)");
            strSql.Append(" values (");
            strSql.Append("@RawId,@OneNormName,@NormId,@TwoNormName,@ElectValue,@Temp1,@Temp2)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@OneNormName", SqlDbType.VarChar,50),
					new SqlParameter("@NormId", SqlDbType.Int,4),
					new SqlParameter("@TwoNormName", SqlDbType.VarChar,50),
					new SqlParameter("@ElectValue", SqlDbType.VarChar,50),
					new SqlParameter("@Temp1", SqlDbType.VarChar,50),
					new SqlParameter("@Temp2", SqlDbType.VarChar,50)};
            parameters[0].Value = model.RawId;
            parameters[1].Value = model.OneNormName;
            parameters[2].Value = model.NormId;
            parameters[3].Value = model.TwoNormName;
            parameters[4].Value = model.ElectValue;
            parameters[5].Value = model.Temp1;
            parameters[6].Value = model.Temp2;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_RawNorm model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RawNorm set ");
            strSql.Append("RawId=@RawId,");
            strSql.Append("OneNormName=@OneNormName,");
            strSql.Append("NormId=@NormId,");
            strSql.Append("TwoNormName=@TwoNormName,");
            strSql.Append("ElectValue=@ElectValue,");
            strSql.Append("Temp1=@Temp1,");
            strSql.Append("Temp2=@Temp2");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@RawId", SqlDbType.Int,4),
					new SqlParameter("@OneNormName", SqlDbType.VarChar,50),
					new SqlParameter("@NormId", SqlDbType.Int,4),
					new SqlParameter("@TwoNormName", SqlDbType.VarChar,50),
					new SqlParameter("@ElectValue", SqlDbType.VarChar,50),
					new SqlParameter("@Temp1", SqlDbType.VarChar,50),
					new SqlParameter("@Temp2", SqlDbType.VarChar,50),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RawId;
            parameters[1].Value = model.OneNormName;
            parameters[2].Value = model.NormId;
            parameters[3].Value = model.TwoNormName;
            parameters[4].Value = model.ElectValue;
            parameters[5].Value = model.Temp1;
            parameters[6].Value = model.Temp2;
            parameters[7].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawNorm ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RawNorm ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_RawNorm GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RawId,OneNormName,NormId,TwoNormName,ElectValue,Temp1,Temp2 from RawNorm ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_RawNorm model = new E_RawNorm();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawId"].ToString() != "")
                {
                    model.RawId = int.Parse(ds.Tables[0].Rows[0]["RawId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OneNormName"] != null)
                {
                    model.OneNormName = ds.Tables[0].Rows[0]["OneNormName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NormId"].ToString() != "")
                {
                    model.NormId = int.Parse(ds.Tables[0].Rows[0]["NormId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TwoNormName"] != null)
                {
                    model.TwoNormName = ds.Tables[0].Rows[0]["TwoNormName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ElectValue"] != null)
                {
                    model.ElectValue = ds.Tables[0].Rows[0]["ElectValue"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Temp1"] != null)
                {
                    model.Temp1 = ds.Tables[0].Rows[0]["Temp1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Temp2"] != null)
                {
                    model.Temp2 = ds.Tables[0].Rows[0]["Temp2"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RawId,OneNormName,NormId,TwoNormName,ElectValue,Temp1,Temp2 ");
            strSql.Append(" FROM RawNorm ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RawId,OneNormName,NormId,TwoNormName,ElectValue,Temp1,Temp2 ");
            strSql.Append(" FROM RawNorm ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  Method
    }
}
